<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/7/13
 * Time: 22:18
 */

namespace App\Api;

use PhalApi\Api;
use App\Domain\Examples\ViewUser as UserDao;

/**
 * ☛☛登录相关√☚☚
 *4.修改手机号
 *5.验证码
 */
class Login extends Api
{
//    protected $appid = 'wxbc2d7b9d1dc1a4f5';
    protected $appid='wxb2d6ecc84a9d6d9f';
    protected $secret = 'a2a80113ed20f5d8dfe256e64a102174';
//    protected $secret = '547183e869672f73cdcb672504285bb7';

    //控制输入参数
    public function getRules()
    {
        return array(
            //方法名
            'login' => array(//name-传过来的参数
                'js_code' => array('name' => 'js_code', 'require' => true, 'desc' => '通过wx.login获得的js_code'),
            ),
            'getCodeByTel' => array(
                'tel' => array('name' => 'tel', 'require' => true, 'desc' => '手机号码'),
            ),
            'check_login' => array(
                'id' => array('name' => 'id', 'require' => true, 'desc' => 'openid'),
            ),
            'getPhoneByOpenId' => array(
                'id' => array('name' => 'id', 'require' => true, 'desc' => 'openid'),
            ),
            'changePhoneNum' => array(
                'tel' => array('name' => 'tel', 'require' => true, 'desc' => '手机号码'),
                'id' => array('name' => 'id', 'require' => true, 'desc' => '登录用户的openId'),
            ),);
    }

    /**
     * 用户登录接口
     * @desc 用户登录接口
     * @return string 成功--openid 用户唯一标识
     * @return string 成功--session_key 会话密钥
     * @return string 成功--unionid 用户在开放平台的唯一标识符
     * @return string 失败--errcode
     * @return string 失败--errmsg
     *
     */
    public function login()
    {
        $token_url = 'https://api.weixin.qq.com/sns/jscode2session?appid=' . $this->appid . '&secret=' . $this->secret . '&js_code=' . $this->js_code . '&grant_type=authorization_code';
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $token_url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        $out = curl_exec($ch);
        curl_close($ch);
        $jsondecode = json_decode($out, true);
        return $jsondecode;
    }

    /**
     * 检测是否登录
     * @desc 根据openid获得当前账号是否已经存在数据库
     * @return boolean true：openid已存在
     */
    public function check_login()
    {
        $domain = new UserDao();
        $result = $domain->getLoginStateByOpenId($this->id);
        return count($result) > 0;
    }

    /**
     * 根据OpenId获得当前账号的手机号
     * @desc 根据OpenId获得当前账号的手机号
     * @return 当前用户手机号
     */
    public function getPhoneByOpenId()
    {
        $domain = new UserDao();
        return $domain->getLoginStateByOpenId($this->id);
    }

    /**
     * 验证码 未对接短信接口
     * @desc 根据手机号获得验证码
     * @return string 发送结果
     */
    public function getCodeByTel()
    {
        $this->tel;
        return 'true';
    }

    /**
     * 修改手机号接口
     * @desc 根据OpenId修改手机好了
     * @return boolean result 添加结果
     * @return string msg 结果描述
     */
    public function changePhoneNum()
    {
        $rs = array();
        $domain = new UserDao();
        $newData['phone']=$this->tel;
        $code = $domain->update($this->id, $newData);
        $result = false;
        $result_content = '更新失败，请稍后重试';
        if ($code > 0) {
            $result = true;
            $result_content = '更新成功';
        }
        $rs['result'] = $result;
        $rs['msg'] = $result_content;
        return $code;
    }
}